package com.adel.earthquake;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ZoomControls;

import com.adel.earthquake.Properties.Features;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MyMapActivity extends MapActivity {
	public static final String MAP_DATA = "com.isman.map.MapActivity.MAPDATA";

	private Features features;
	private MapView mapView;
	private MapController mapControll;
	private MyItemizedOverlay currentOverlay;
	private TextView tvTitle;
	private TextView tvSubTitle;
	private MyItemizedOverlay cOverlay;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);
		setTitle(R.string.app_name);

		features = (Features) getIntent().getExtras().getSerializable(MAP_DATA);
		Date date = new Date();
		date.setTime((long)(features.getProperties().getTime() * 1000));
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	
	
		Log.v("Isman", features.getProperties().getUrl());
		
		tvTitle = (TextView)findViewById(R.id.map_title);
		tvSubTitle = (TextView)findViewById(R.id.map_subtitle);
		tvTitle.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(features.getProperties().getUrl()));
				startActivity(browserIntent);
			}
		});
		tvSubTitle.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(features.getProperties().getUrl()));
				startActivity(browserIntent);
			}
		});
		tvTitle.setText(features.getProperties().getPlace());
		tvSubTitle.setText("Depth : "+features.getGeometry().getCoordinates()[2]+" Km. Date : "+dateFormat.format(date));

		
		
		

		
			
		
		mapView = (MapView) findViewById(R.id.map);
		mapControll = mapView.getController();
		mapView.setBuiltInZoomControls(true);
		mapView.setClickable(true);
		
		ZoomControls zoomControls = (ZoomControls) findViewById(R.id.zoomcontrols);
		zoomControls.setOnZoomInClickListener(new OnClickListener() {
		        public void onClick(View v) {
		            mapView.getController().zoomIn();
		        }
		});
		zoomControls.setOnZoomOutClickListener(new OnClickListener() {
		        public void onClick(View v) {
		            mapView.getController().zoomOut();
		        }
		});
		
		try {

			getCurrentLocation();

			
			
			
			
			GeoPoint geoPoint = new GeoPoint((int) (features.getGeometry()
					.getCoordinates()[1] * 1E6), (int) (features.getGeometry()
					.getCoordinates()[0] * 1E6));
			mapControll.animateTo(geoPoint);

			Drawable current = this.getResources().getDrawable(
					R.drawable.google_maps_marker);

			currentOverlay = new MyItemizedOverlay(current);
			currentOverlay.addOverlay(new OverlayItem(geoPoint, features.getProperties().getPlace(),
					null));
			mapView.getOverlays().add(currentOverlay);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void getCurrentLocation() {
		LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		LocationListener ll = new LocationListener() {
			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProviderEnabled(String provider) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onLocationChanged(final Location location) {
				if(location != null){
					GeoPoint geoPoint = new GeoPoint((int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6));
					Drawable current = MyMapActivity.this.getResources().getDrawable(R.drawable.ic_maps_indicator_current_position);

					if(cOverlay != null){
						mapView.getOverlays().remove(cOverlay);
					}
					
					cOverlay = new MyItemizedOverlay(current);
					cOverlay.addOverlay(new OverlayItem(geoPoint, "I'm Here!!!",null));
					mapView.getOverlays().add(cOverlay);
					
					
				}
			}
		};
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
	}

	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	public static class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {

		private ArrayList<OverlayItem> myOverlays;

		public MyItemizedOverlay(Drawable defaultMarker) {
			super(boundCenterBottom(defaultMarker));
			myOverlays = new ArrayList<OverlayItem>();
			populate();
		}

		public void addOverlay(OverlayItem overlay) {
			myOverlays.add(overlay);
			populate();
		}

		@Override
		protected OverlayItem createItem(int i) {
			return myOverlays.get(i);
		}

		// Removes overlay item i
		public void removeItem(int i) {
			myOverlays.remove(i);
			populate();
		}

		// Returns present number of items in list
		@Override
		public int size() {
			return myOverlays.size();
		}

		public void addOverlayItem(OverlayItem overlayItem) {
			myOverlays.add(overlayItem);
			populate();
		}

		public void addOverlayItem(int lat, int lon, String title) {
			try {
				GeoPoint point = new GeoPoint(lat, lon);
				OverlayItem overlayItem = new OverlayItem(point, title, null);
				addOverlayItem(overlayItem);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}

		@Override
		protected boolean onTap(int index) {
			return super.onTap(index);
		}
	}
}
